package com.baihan.angus.app.game.busshow.adps;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.baihan.angus.app.game.basic.BaseApplication;
import com.baihan.angus.app.game.basic.base.BaseAdp;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.common.bean.response.RankingResponse;
import com.baihan.angus.app.game.common.util.ImageLoader;
import com.qmuiteam.qmui.widget.QMUIRadiusImageView;

public class RankingAdp extends BaseAdp<RankingResponse.DataX.Data, RankingAdp.ViewHolder> {

    private OnItemClickListener mOnItemClickListener;
    private int star;

    @Override
    protected RecyclerView.ViewHolder createYourViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rank_script, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void bindYourViewHolder(@NonNull ViewHolder holder, int position) {
        ViewHolder vh = holder;

        RankingResponse.DataX.Data data = getItem(position);

        if (null != data) {
            vh.tvScriptName.setText(data.getName());
            vh.tvScore.setText(data.getScore());
            vh.tvIntro.setText(data.getIntro());

            ImageLoader.loadImage(BaseApplication.getApplication().getApplicationContext(),
                    vh.ivScriptCover, data.getCover(), R.mipmap.ic_test_bg, 0);
        }

        if (position == 0) {
            vh.tvSort.setVisibility(View.GONE);
            vh.ivMedal.setVisibility(View.VISIBLE);
            vh.ivMedal.setImageResource(R.mipmap.ic_gold_medal);
        } else if (position == 1) {
            vh.tvSort.setVisibility(View.GONE);
            vh.ivMedal.setVisibility(View.VISIBLE);
            vh.ivMedal.setImageResource(R.mipmap.ic_silver_medal);
        } else if (position == 2) {
            vh.tvSort.setVisibility(View.GONE);
            vh.ivMedal.setVisibility(View.VISIBLE);
            vh.ivMedal.setImageResource(R.mipmap.ic_bronze_medal);
        } else {
            vh.tvSort.setVisibility(View.VISIBLE);
            vh.ivMedal.setVisibility(View.GONE);
            vh.tvSort.setText(position + 1 + ".");
        }

        float rate = Float.parseFloat(data.getScore());
        vh.rating.setMax(10);
        star = ((int) rate) / 2;
        vh.rating.setRating(star);

        vh.llItem.setOnClickListener(view -> {
            mOnItemClickListener.onItemClick(position);
        });
    }

    @Override
    protected int getEmptyView() {
        return 0;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView ivMedal;
        QMUIRadiusImageView ivScriptCover;
        TextView tvScriptName, tvScore, tvIntro, tvSort;
        RatingBar rating;
        LinearLayout llItem;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ivMedal = itemView.findViewById(R.id.ivMedal);
            ivScriptCover = itemView.findViewById(R.id.ivScriptCover);
            tvScriptName = itemView.findViewById(R.id.tvScriptName);
            tvScore = itemView.findViewById(R.id.tvScore);
            tvIntro = itemView.findViewById(R.id.tvIntro);
            tvSort = itemView.findViewById(R.id.tvSort);
            rating = itemView.findViewById(R.id.rating);
            llItem = itemView.findViewById(R.id.llItem);
        }
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }
}
